package com.jiyinit.web.core.business.service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.business.dao.TabCoreFunDao;
import com.jiyinit.web.core.business.dao.TabCoreOrgDao;
import com.jiyinit.web.core.business.dao.TabFunRoleDao;
import com.jiyinit.web.core.business.dao.TabFunRoleRelationDao;
import com.jiyinit.web.core.business.dto.FunRolePageRequest;
import com.jiyinit.web.core.business.dto.FunRoleRequest;
import com.jiyinit.web.core.business.entity.TabFunRoleEntity;
import com.jiyinit.web.core.business.entity.TabFunRoleRelationEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.vo.FunctionRoleVo;
import com.jiyinit.web.core.business.vo.FunctionVo;
import com.jiyinit.web.core.common.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lr on 2017/12/21.
 */
@Service("function.funRoleServiceImpl")
public class FunRoleServiceImpl {

    @Resource(name = "core.tabFunRoleDao")
    private TabFunRoleDao tabFunRoleDao;
    @Resource(name ="core.tabCoreOrgDao")
    private TabCoreOrgDao tabCoreOrgDao;

    @Resource(name = "core.tabCoreFunDao")
    private TabCoreFunDao tabCoreFunDao;

    @Resource(name="core.tabFunRoleRelationDao")
    private TabFunRoleRelationDao tabFunRoleRelationDao;

    public List<FunctionVo> findOrgFunction(Integer orgId,Integer funRoleId){
        return tabCoreFunDao.findOrgFunction(orgId,funRoleId);
    }

    public PagingResult<FunctionRoleVo> findByPage(Integer orgId,FunRolePageRequest funRolePageRequest){
        Integer topOrgId = tabCoreOrgDao.findTopOrg(orgId);
        return tabFunRoleDao.findByPage(topOrgId,funRolePageRequest);
    }

    public int add(FunRoleRequest request){
        Integer orgId = UserUtil.getCurrentUser().getOrgId();
        Integer topOrgId = tabCoreOrgDao.findTopOrg(orgId);
        TabFunRoleEntity entity = new TabFunRoleEntity();
        entity.setFunRoleName(request.getFunRoleName());
        entity.setFunRoleDesc(request.getFunRoleDesc());
        entity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        entity.setOrgId(topOrgId);
        entity.setCreateId(UserUtil.getCurrentUser().getUserId());
        entity.setCreateTime(new Date());
        entity.setFunRoleType(CommonEnum.FunRoleType.OTHERS.getCode());
        return tabFunRoleDao.add(entity);
    }

    public TabFunRoleEntity findById(Integer funRoleId){
        return tabFunRoleDao.findById(funRoleId);
    }

    public int update(FunRoleRequest request){
        TabFunRoleEntity entity = new TabFunRoleEntity();
        entity.setFunRoleId(request.getFunRoleId());
        entity.setFunRoleName(request.getFunRoleName());
        entity.setFunRoleDesc(request.getFunRoleDesc());
        entity.setLastModifyId(UserUtil.getCurrentUser().getUserId());
        entity.setLastModifyTime(new Date());
        return tabFunRoleDao.update(entity);
    }

    public List<TabFunRoleEntity> findByIds(Integer[] funRoleIds){
        return tabFunRoleDao.findByIds(funRoleIds);
    }

    public int delete(Integer[] funRoleIds){

        List<TabFunRoleEntity> list =new ArrayList<TabFunRoleEntity>();
        for (Integer id:funRoleIds){
            TabFunRoleEntity entity = new TabFunRoleEntity();
            entity.setFunRoleId(id);
            entity.setIsDelete(CommonEnum.IsDelete.DELETED.getCode());
            list.add(entity);
        }
        return tabFunRoleDao.delete(list);
    }

    public int updateRoleFunction(Integer orgId,Integer funRoleId,Integer[] funIds){
        Integer userId = UserUtil.getCurrentUser().getUserId();
        //删除
         tabFunRoleRelationDao.delete(orgId,funRoleId,userId);
        //添加
        List<TabFunRoleRelationEntity> list = new ArrayList<>();
        for(Integer id : funIds){
            TabFunRoleRelationEntity entity = new TabFunRoleRelationEntity();
            entity.setFunId(id);
            entity.setOrgId(orgId);
            entity.setFunRoleId(funRoleId);
            entity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            entity.setCreateId(userId);
            entity.setCreateTime(new Date());
            list.add(entity);
        }
        return tabFunRoleRelationDao.batchAdd(list);
    }

}
